<?php 
	include("../conn.php");
	mysql_query('SET NAMES UTF8'); 
	

	$result_data_array = array();

	$userid = $_REQUEST['userid'];
	$shopid = $_REQUEST['shopid'];

	if(!$userid && !$shopid){
		$result_data_array['code'] = -100;
		$result_data_array['msg'] = '用户信息错误';
		echo json_encode($result_data_array);
		exit;
	}

	$page = $_REQUEST['page'];
	$pagesize = $_REQUEST['pagesize'];
	if(!$page){
		$page = 0;
	}
	if(!$pagesize){
		$pagesize = 10;
	}



	$orders_sql = "select "
	."a.ordersid,a.totalprice,a.return_volume_price,a.coupon_price,a.state,a.distribution_fee,a.shopid,a.kuaidi,a.numbers,a.freightRisk_price,a.buyFreightRisk,a.danhao,a.xia_dan_datetime"
	.",b.name,b.images as images"
	." from goodsorders as a left join shop as b on b.shopid = a.shopid"
	." where a.userid = ".$userid."  and a.state = 5 and b.open= 1 and b.check = 1"
	." order by a.ordersid desc limit ".$page*$pagesize.",$pagesize";

	$orders_query = mysql_query($orders_sql);
	$orders_data_array = array();



	while ($orders_row = mysql_fetch_assoc($orders_query)) {
		$car_sql = "select "
		."a.carid,a.goodsid,a.shopid,a.fTitle,a.price as car_price,a.sTitle,a.number as car_number"
		.",b.start_num,b.shangjia, b.number as goods_number,b.yuanjia, b.price as goods_price,b.title,b.images"
		." from car as a "
		." left join goods as b on b.goodsid = a.goodsid "
		." where a.ordersid = ".$orders_row['ordersid'];
		$car_query = mysql_query($car_sql);

		$total_goods_num = 0;
		$car_array = array();
		while ($car_row = mysql_fetch_assoc($car_query)) {
			$fTitle = $car_row['fTitle'];
			$sTitle = $car_row['sTitle'];
			$total_goods_num+=$car_row['car_number'];
			//规格不存在改变,1没改变
			$car_row['goodsselect_change'] = 1;

			if( $fTitle  != null && $fTitle != ''){
				$goodsselect_sql = "select price,number from goodsselect where goodsid = ".$car_row['goodsid']." and fTitle = '".$fTitle."'";
				if($sTitle != null && $sTitle != ''){
					$goodsselect_sql .= " and sTitle = '".$sTitle."'";
				}

				$goodsselect_query = mysql_query($goodsselect_sql);
				$goodsselect_row = mysql_fetch_assoc($goodsselect_query);
				if($goodsselect_row){
					$car_row['goods_price'] = $goodsselect_row['price'];
					$car_row['goods_number'] = $goodsselect_row['number'];
				}else{
					$car_row['goods_price'] = 	$car_row['car_price'];
					$car_row['goodsselect_change'] = 0;
				}


			}
			$car_row['images'] = explode(',',$car_row['images'])[0];
			$car_array[] = $car_row;
		}
		$orders_row['order_total_price'] = round($orders_row['totalprice']*100 + $orders_row['distribution_fee']*100 - $orders_row['return_volume_price']*100 - $orders_row['coupon_price']*100)/100;
		if((int)$orders_row['buyFreightRisk'] == 1){
			$orders_row['order_total_price'] = round($orders_row['order_total_price']*100 + $orders_row['freightRisk_price']*100)/100;
		}

		$orders_row['total_goods_num'] = $total_goods_num;
		$orders_row['goods_list'] = $car_array;
		$orders_data_array[] = $orders_row;


	}
	
	$result_data_array['code'] = 200;
	$result_data_array['msg'] = '获取成功';
	$result_data_array['data'] = $orders_data_array;
	echo json_encode($result_data_array);


	
 ?>